import { defineStore } from 'pinia'
import type { RouteLocationNormalized } from 'vue-router'

type TabItem = {
    path: string
    title: string
    fullPath: string
}

export const useTabsStore = defineStore('tabs', {
    state: () => ({
        tabs: [] as TabItem[],
    }),
    actions: {
        // 添加标签页
        addTab(route: RouteLocationNormalized) {
            if (this.tabs.some(tab => tab.path === route.path)) return

            this.tabs.push({
                path: route.path,
                title: route.meta.title as string || '未命名',
                fullPath: route.fullPath
            })
        },

        // 移除标签页
        removeTab(path: string) {
            this.tabs = this.tabs.filter(tab => tab.path !== path)
            if (this.activeTab === path) {
                this.activeTab = this.tabs[this.tabs.length - 1]?.path || '/'
            }
        },

        // 设置当前激活标签
        setActiveTab(path: string) {
            this.activeTab = path
        }
    }
})